package org.com.lcuiot.service;


import org.com.lcuiot.database.dto.Result;
import org.com.lcuiot.database.entity.Product;
import org.com.lcuiot.database.mapper.ProductMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 设备服务
 *
 * @author gaoge
 * @since 2022/10/27 14:31
 */
@Service
public class ProductService {
    private static final Logger logger = LoggerFactory.getLogger(ProductService.class);
    private final ProductMapper productMapper;

    @Autowired
    public ProductService(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    public List<Product> selectAll() {
        Product product = new Product();
        return productMapper.selectList(product);

    }

    /**
     * 根据机构查询所有产品信息
     *
     * @param organizationId 请求信息
     * @return Result<Object>
     */
    public Result<Object> selectProductByOrganizationId(Long organizationId) {
        Result<Object> result = Result.build();
        try {
            Product product = new Product();
            product.setOrganizationId(organizationId);
            List<Product> products = productMapper.selectList(product);
            result.setData(products);
        } catch (Exception e) {
            logger.error("查询所有产品信息发生异常:", e);
            result.setCode(500).setMsg("查询所有产品信息信息发生异常");
        }
        return result;
    }

    /**
     * 根据产品ID查询产品信息
     *
     * @param productId 请求信息
     * @return Result<Object>
     */
    public Result<Object> selectByProductId(String productId) {
        Result<Object> result = Result.build();
        try {
            Optional<Product> product = productMapper.selectByPrimaryKey(productId);
            product.ifPresent(result::setData);
        } catch (Exception e) {
            logger.error("根据产品id查询产品信息发生异常:", e);
            result.setCode(500).setMsg("根据产品id查询产品信息发生异常");
        }
        return result;
    }


}
